<?php

namespace app\admin\controller;
use app\BaseController;
use think\facade\Db;

class Goods extends BaseController {

  public function info(){
    $id = request()->param('id');
    if (!$id) {
      returnJson(-2, '参数错误');
    }
    $data = Db::table('shop_goods')->where('id', $id)->find();
    if (!$data) {
      returnJson(-2, '查询失败！');
    }
    returnJson(0, '成功！', $data);
  }

  public function all_list(){
    $data = Db::table('shop_goods')->select();
    if (!$data) {
      returnJson(-2, '查询失败！');
    }
    returnJson(0, '成功！', $data);
  }

  public function list() {
    $request = request()->param();

    //分页查询
    $page = pageQuery($request);

    $goodsList = Db::table('shop_goods')->order('sort', 'asc')->page($page['page'], $page['count'])->select();
    if (!$goodsList) {
      returnJson(-2, '查询失败！');
    }
    $goodsCount = Db::table('shop_goods')->count(); //总条数

    $categoryList = Db::table('shop_category')->order('id', 'asc')->select();
    if($categoryList){
      $categoryList = $categoryList->toArray();
      $id = array_column($categoryList, 'id');
      $name = array_column($categoryList, 'category_name');
      $categoryList = array_combine($id, $name);
    }
    
    $data = [
      'count' => $page['count'],
      'total' => $goodsCount,
      'rows' => $goodsList,
      'categoryList' => $categoryList
    ];
    returnJson(0, '成功！', $data);
  }

  public function add() {
    $request = request()->param();

    $data = [
      'name' => $request['name'],
      'category_id' => request()->param('category_id'),
      'price' => request()->param('price')?request()->param('price'):0,
      'number' => request()->param('number'),
      'act_price' => request()->param('act_price')?request()->param('act_price'):0,
      'img' => request()->param('img'),
      'note' => request()->param('note'),
      'content' => request()->param('content'),
      'sort' => isset($request['sort'])?$request['sort']:2000,
    ];

    $save = Db::table('shop_goods')->save($data);
    if ($save) {
      returnJson(0, '添加成功！');
    }
    returnJson(-2, '添加失败！');
  
  }

  public function update (){
    $request = request()->param();
    if (!isset($request['name']) || !isset($request['id'])) {
      returnJson(-2, '参数错误');
    }

    $id = $request['id'];
    $data = [
      'name' => $request['name'],
      'category_id' => request()->param('category_id'),
      'price' => request()->param('price')?request()->param('price'):0,
      'number' => request()->param('number'),
      'act_price' => request()->param('act_price')?request()->param('act_price'):0,
      'img' => request()->param('img'),
      'note' => request()->param('note'),
      'content' => request()->param('content'),
      'sort' => isset($request['sort'])?$request['sort']:2000,
    ];
    
    $update = Db::table('shop_goods')->where('id', $id)->update($data);
    if ($update >= 0) {
      returnJson(0, '修改成功！');
    }
    returnJson(-2, '修改失败！');
  }

  public function delete () {
    $ids = request()->param('ids');
    $idsArr = explode(',', $ids);
    
    if (is_array($idsArr)){
      $del = Db::table('shop_goods')->delete($idsArr);
      returnJson(0, '删除成功！');
    }
    returnJson(-2, '删除失败！');
  }




}


